﻿<%@ WebHandler Language="C#" Class="AjaxGetLeftMenu" %>

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.SessionState;
using Business;
using Business.Common;
using Business.Menu;
using Business.Model;

public class AjaxGetLeftMenu : IHttpHandler, IRequiresSessionState
{
    private readonly XtMenuOperation clsMenu = new XtMenuOperation();

    public void ProcessRequest(HttpContext context)
    {
        var firstMenuName = context.Request["firstMenuName"];
        context.Response.ContentType = "text/plain";
        context.Response.Write(GenerateBodyScript(context, firstMenuName));
    }

    public bool IsReusable
    {
        get { return false; }
    }

    public string GenerateBodyScript(HttpContext context, string firstMenuName)
    {
        if (context.Session["UserId"] == null)
        {
            return "UserId为空";
        }
        var userid = Convert.ToString(context.Session["UserId"]);
        var dtParent = clsMenu.GenerateParentMenu(firstMenuName);
        var listParent = new List<ParentMenu>();
        dtParent.Rows.Cast<DataRow>().ToList().ForEach(row =>
        {
            var dtChild = clsMenu.GenerateChildMenu(row["menu_parent_id"].ToString(), userid);
            var listChild = new List<ChildMenu>();
            if (dtChild.Rows.Count == 0) return;
            dtChild.Rows.Cast<DataRow>().ToList().ForEach(child =>
            {
                var childMenu = new ChildMenu
                {
                    MenuChildId = child["menu_child_id"].ToString(),
                    MenuChildDesc = child["menu_child_desc"].ToString(),
                    LinkHref = child["linkhref"].ToString(),
                    OverPic = child["overpic"].ToString(),
                    ShowNo = child["show_no"].ToString()
                };
                listChild.Add(childMenu);
            });
            var parentMenu = new ParentMenu
            {
                MenuParentId = row["menu_parent_id"].ToString(),
                MenuParentDesc = row["menu_parent_desc"].ToString(),
                PicPath = row["picpath"].ToString(),
                ChildMenus = listChild
            };
            listParent.Add(parentMenu);
        });
        return listParent.T2Json();
    }
}